package com.smm.web.vmc.controller.admin;

import com.smm.web.vmc.domain.Admin;
import com.smm.web.vmc.service.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhenghao on 2016/9/6.
 *
 * 管理员登陆
 */
@Controller
public class AdminLoginController {

    @Resource
    private AdminService adminService;

    @Resource
    private WarehouseController warehouseController;


    @RequestMapping("/adminLogin.do")
    public ModelAndView login(){

        return new ModelAndView("admin/login");
    }


    @RequestMapping("/adminSubmitLogin.do")
    public ModelAndView submitLogin(HttpServletRequest request,String account,String password){

        //管理员登陆
        try{

            if(StringUtils.isBlank(account)) throw new Exception("登陆账号不能为空");

            if(StringUtils.isBlank(password)) throw new Exception("登陆密码不能为空");

            Admin admin = adminService.login(account,password);

            if(admin==null) throw new Exception("登陆账号、密码错误");

            //登陆成功，保存到session
            request.getSession().setAttribute(AdminService._ADMIN_IN_SESSION,admin);

            //登陆成功，跳转到仓库管理页面
            return warehouseController.list(request);

        }catch (Exception e){

            e.printStackTrace();

            ModelAndView mv = new ModelAndView("admin/login");

            mv.addObject("err_msg",e.getMessage());

            return mv;

        }

    }



}
